package com.heba.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.heba.Debug;
import com.heba.dialog.MyProgressDialog;
import com.heba.greenmulu.R;
import com.heba.utils.DensityUtils;
import com.heba.view.Loading;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

/**
 * BaseFragment
 * <p>
 * Created by shenghao on 2015/6/10.
 */
public class BaseFragment extends Fragment {

    protected final String TAG = this.getClass().getSimpleName();

    //屏幕宽高
    protected int ScreenWidth = 0;   //屏幕宽度
    protected int ScreenHeight = 0;  //屏幕高度

    //分页显示
    protected int PerPage = 20;   //每页加载的数据
    protected int Page = 1;    //加载页

    //PullToRefreshListView加载
    protected boolean firstRefresh = true;   //判断是否第一次刷新
    protected boolean isRefreshFromTop = false;  //判断是否下拉刷新
    protected boolean isLoadMore = true;     //加载更多

    //加载框
    protected MyProgressDialog progressDialog;
    protected Loading loading;

    /**
     * Fragment当前状态是否可见
     */
    protected boolean isVisible = false;

    /**
     * Fragment的view是否已创建
     */
    protected boolean mIsViewCreated = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Debug.LogError(TAG, "onCreateView");
        View view = inflater.inflate(R.layout.fragment_content, container, false);
        mIsViewCreated = true;
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mIsViewCreated = false;
    }

    @Override
    public void onResume() {
        Debug.LogError(TAG, "onResume");
        //和activity的onResume绑定，Fragment初始化的时候必调用，但切换fragment的hide和visible的时候可能不会调用！
        super.onResume();
        if (isAdded() && !isHidden()) {
            //用isVisible此时为false，因为mView.getWindowToken为null
            onVisible();
            isVisible = true;
        }
    }

    @Override
    public void onPause() {
        Debug.LogError(TAG, "onPause");
        if (isVisible() || isVisible) {
            onHidden();
            isVisible = false;
        }
        super.onPause();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        Debug.LogError(TAG, "setUserVisibleHint");
        super.setUserVisibleHint(isVisibleToUser);
        if (!mIsViewCreated) {
            //view没有创建的时候不进行操作
        } else {
            if (isVisibleToUser) {
                if (!isVisible) {
                    //确保在一个可见周期中只调用一次onVisible()
                    isVisible = true;
                    onVisible();
                }
            } else {
                if (isVisible) {
                    isVisible = false;
                    onHidden();
                }
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        //默认fragment创建的时候是可见的，但是不会调用该方法！切换可见状态的时候会调用，但是调用onResume，onPause的时候却不会调用
        super.onHiddenChanged(hidden);
        if (hidden) {
            // 不在最前端显示 相当于调用了onPause();
            Debug.LogError(TAG, "onHiddenChanged onHidden()");
            onHidden();
            isVisible = false;
        } else {
            // 在最前端显示 相当于调用了onResume();
            Debug.LogError(TAG, "onHiddenChanged onVisible()");
            onVisible();
            isVisible = true;
        }
    }

    /**
     * fragment可见的时候操作，取代onResume，且在不可见状态切换到可见状态的时候调用
     */
    protected void onVisible() {

    }

    /**
     * fragment不可见的时候操作,onPause的时候,以及不可见的时候调用
     */
    protected void onHidden() {

    }

    /**
     * init
     */
    private void init() {
        ScreenWidth = DensityUtils.getWidth(getActivity());
        ScreenHeight = DensityUtils.getHeight(getActivity());

        loading = new Loading(getActivity());
        progressDialog = new MyProgressDialog(getActivity(), getActivity().getResources().getString(R.string.loading), new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                Debug.Log("Dialog OnCancelListener", "cancel click");
            }
        });

    }

    /**
     * 通过类名启动Activity
     *
     * @param pClass
     */
    protected void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    /**
     * 通过类名启动Activity，并且含有Bundle数据
     *
     * @param pClass
     * @param pBundle
     */
    protected void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(getActivity(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    /**
     * 进入activity添加fade in动画
     *
     * @param pClass
     * @param pBundle
     */
    protected void openActivityWithFadeInAnimation(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(getActivity(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    /**
     * 动画退出activity
     */
    protected void finishActivityWithFadeInAnimation() {
        getActivity().finish();
        getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    /**
     * 通过Action启动Activity
     *
     * @param pAction
     */
    protected void openActivity(String pAction) {
        openActivity(pAction, null);
    }

    /**
     * 通过Action启动Activity
     *
     * @param pAction
     * @param url
     */
    protected void openActivityFromUri(String pAction, String url) {
        Intent intent = new Intent(pAction, Uri.parse(url));
        startActivity(intent);
    }

    /**
     * 通过Action启动Activity，并且含有Bundle数据
     *
     * @param pAction
     * @param pBundle
     */
    protected void openActivity(String pAction, Bundle pBundle) {
        Intent intent = new Intent(pAction);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    /**
     * 关闭刷新加载
     *
     * @param refreshLayout
     */
    protected void closeRefreshView(RefreshLayout refreshLayout) {
        if (refreshLayout.getState().isOpening) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
